package com.easondongh.system.controller;

import com.easondongh.system.constants.Constants;
import com.easondongh.system.domain.SysRole;
import com.easondongh.common.enums.ResponseEnum;
import com.easondongh.common.response.ResponseObj;
import com.easondongh.system.service.SysRoleService;
import com.easondongh.system.utils.StringUtils;
import com.easondongh.common.vo.BasePageQueryVo;
import com.easondongh.common.vo.PageResult;
import com.easondongh.system.vo.RoleDispatchVo;
import com.easondongh.system.vo.RoleQueryVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * @author EasonDongH
 * @date 2020/5/11 9:24
 */
@Slf4j
@RestController
@RequestMapping("/role")
public class RoleController {

    @Autowired
    private SysRoleService roleService;

    /**
     * 加载所有启用中的角色
     * @return
     */
    @GetMapping("/loadEnableRole")
    public ResponseObj<List<SysRole>> loadEnableRole() {
        List<SysRole> roles = this.roleService.loadEnableRole();
        return new ResponseObj<>(ResponseEnum.QUERY_SUCCESS, roles);
    }

    @PostMapping("/getByPage")
    public ResponseObj<PageResult<SysRole>> getByPage(@RequestBody BasePageQueryVo<RoleQueryVo> queryVo) {
        if(queryVo == null) {
            return ResponseObj.QueryFail;
        }
        PageResult<SysRole> result = this.roleService.getByPage(queryVo);
        return new ResponseObj<>(ResponseEnum.QUERY_SUCCESS, result);
    }

    @PostMapping("/addRole")
    public ResponseObj addRole(@RequestBody SysRole role) {
        if(role == null || StringUtils.isBlank(role.getName())) {
            log.error("新增角色时角色名称必填");
            return ResponseObj.AddFail;
        }
        role.setCreateTime(new Date());
        this.roleService.save(role);
        return ResponseObj.AddSuccess;
    }

    @PutMapping("/enable/{id}")
    public ResponseObj enable(@PathVariable("id") Integer id) {
        if(id == null) {
            return ResponseObj.UpdateFail;
        }
        SysRole role = new SysRole();
        role.setId(id);
        role.setEnable(Constants.ENABLE);
        this.roleService.updateById(role);
        return ResponseObj.UpdateSuccess;
    }

    @PutMapping("/disable/{id}")
    public ResponseObj disable(@PathVariable("id") Integer id) {
        if(id == null) {
            return ResponseObj.UpdateFail;
        }
        SysRole role = new SysRole();
        role.setId(id);
        role.setEnable(Constants.DISABLE);
        this.roleService.updateById(role);
        return ResponseObj.UpdateSuccess;
    }

    @PutMapping("/updateById")
    public ResponseObj updateById(@RequestBody SysRole role) {
        if(role == null || StringUtils.isBlank(role.getName())) {
            log.warn("修改角色时id、角色名不能为空");
            return ResponseObj.UpdateFail;
        }
        this.roleService.updateById(role);
        return ResponseObj.UpdateSuccess;
    }

    @DeleteMapping("/deleteById")
    public ResponseObj deleteById(Integer id) {
        if(id != null) {
            this.roleService.removeById(id);
        }
        return ResponseObj.DeleteSuccess;
    }

    /**
     * 分配可见菜单、可用权限
     * @return
     */
    @PutMapping("/dispatchMenuAndPermission")
    public ResponseObj dispatchMenuAndPermission(@RequestBody RoleDispatchVo dispatchVo) {
        if(dispatchVo == null || dispatchVo.getRid() == null) {
            log.warn("为角色分配可见菜单、可用权限时，角色id不能为空");
            return ResponseObj.UpdateFail;
        }
        this.roleService.dispatchMenuAndPermission(dispatchVo);
        return ResponseObj.UpdateSuccess;
    }

    /**
     * 获取当前角色所有的可见菜单、可用权限
     */
    @GetMapping("/getMenuAndPermission")
    public ResponseObj<RoleDispatchVo> getMenuAndPermission(Integer rid) {
        if(rid == null) {
            log.warn("获取当前角色所有的可见菜单、可用权限时角色id不能为空");
            return ResponseObj.QueryFail;
        }
        RoleDispatchVo dispatchVo = this.roleService.getMenuAndPermission(rid);
        return new ResponseObj<RoleDispatchVo>(ResponseEnum.QUERY_SUCCESS, dispatchVo);
    }
}
